<!--
 * Description: 塑料粒修改模块
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Qiuxue.Wu
 * Date: 2020/10/10 17:32
-->

<template>
  <div>
    <!--数据表单-->
    <el-form label-width="120px" :model="formData" size="mini">
      <el-row>
        <!--LCFC machine rate for injection--->
        <el-col :span="24">
          <el-form-item label="LCFC machine rate for injection">
            <el-input v-model="formData.machineRate" clearable />
          </el-form-item>
        </el-col>
        <!--Normal($/M)--->
        <el-col :span="24">
          <el-form-item label="Normal($/M)">
            <el-input v-model="formData.normalM" clearable />
          </el-form-item>
        </el-col>
        <!--Normal($/s)--->
        <el-col :span="24">
          <el-form-item label="Normal($/s)">
            <el-input v-model="formData.normalS" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span style="display: block;text-align: center;margin-top: 5px">
      <!--取消按钮-->
      <el-button size="medium" @click="cancel">{{ $t('button.cancel') }}</el-button>
      <!--保存按钮-->
      <el-button :loading="isRequesting" size="medium" type="primary" @click="save">
        {{ $t('button.save') }}
      </el-button>
    </span>
  </div>
</template>

<script>
import { modelExtraConfigEdit } from '@/api/model/extra-config'
export default {
  name: 'MachineTonnageEdit',
  props: {
    rowData: {
      type: Object,
      default: () => {}
    }
  },

  data() {
    return {
      // 正在查询
      isRequesting: false,

      formData: {}
    }
  },

  created() {
    this.formData = this.$deepCopy(this.rowData)
  },

  methods: {
    async save() {
      try {
        const configValue = {}

        for (const key in this.formData) {
          if (key === 'id') {
            continue
          }
          configValue[key] = this.formData[key]

          if (!configValue[key]) {
            return this.$message({
              message: '请填写完整',
              type: 'error',
              duration: 1500
            })
          }
        }

        this.isRequesting = true
        await modelExtraConfigEdit(
          {
            id: this.formData.id,
            configValue: JSON.stringify(configValue)
          },
          { msgSuccess: true, msgError: true }
        )
        this.$emit('save')
      } catch (error) {
        console.log(error)
      } finally {
        this.isRequesting = false
      }
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>
